#coding=utf-8
import unittest
import os, sys
parentdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, parentdir)
from api.base_api.login_base_api import LoginBaseApi
from api.base_api.my_info import MyInfo
from fixture import user

class MyInfoTest(LoginBaseApi):
    ''' 测试用户首页 '''

    def setUp(self):
        self.car_apply_url = "http://192.168.1.141:8001/my-info"
        self.renting_contract_mobile = '15011353321'
        self.finished_contract_mobile = '13800000002'
        self.none_contract_mobile = '13651020524'
        self.password = '123abc'

    def test_contract_reting(self):
        ''' 租赁中 '''

        header = LoginBaseApi().login_base_api(self.renting_contract_mobile, self.password)
        payload =MyInfo().my_info()
        response = self.post(self.car_apply_url, payload, header)
        status_code = response.status_code
        self.assertEqual(status_code, 200)

        response_mobile = response.json()['mobile']
        self.assertEqual(response_mobile, self.renting_contract_mobile)

        response_gender = response.json()['gender']
        db_gender =user.select_user_info(self.renting_contract_mobile)['gender']
        self.assertEqual(response_gender, db_gender)

        response_real_name = response.json()['realName']
        db_real_name = user.select_user_info(self.renting_contract_mobile)['real_name']
        self.assertEqual(response_real_name, db_real_name)

        response_contract_status = response.json()['carStatus']
        db_contract_status = user.select_contract_status(self.renting_contract_mobile)['status']
        self.assertEqual(response_contract_status, db_contract_status)

    def test_contract_finished(self):
        '''已完成,提前还款，已终止'''

        header = LoginBaseApi().login_base_api(self.finished_contract_mobile, self.password)
        payload = MyInfo().my_info()
        response = self.post(self.car_apply_url, payload, header)
        status_code = response.status_code
        self.assertEqual(status_code, 200)

        response_mobile = response.json()['mobile']
        self.assertEqual(response_mobile, self.finished_contract_mobile)

        response_gender = response.json()['gender']
        db_gender = user.select_user_info(self.finished_contract_mobile)['gender']
        self.assertEqual(response_gender, db_gender)

        response_real_name = response.json()['realName']
        db_real_name = user.select_user_info(self.finished_contract_mobile)['real_name']
        self.assertEqual(response_real_name, db_real_name)

        response_contract_status = response.json()['carStatus']
        db_contract_status = user.select_contract_status(self.finished_contract_mobile)['status']
        finished_contract_list = ['COMPLETED','ADVANCE_REPAID','TERMINATED']
        for db_contract_status in finished_contract_list:
            db_contract_status = 'FINISHED'
        self.assertEqual(response_contract_status, db_contract_status)


    def test_contract_none(self):
        '''无合同'''

        header = LoginBaseApi().login_base_api(self.none_contract_mobile, self.password)
        payload = MyInfo().my_info()
        response = self.post(self.car_apply_url, payload, header)
        status_code = response.status_code
        self.assertEqual(status_code, 200)

        response_mobile = response.json()['mobile']
        self.assertEqual(response_mobile, self.none_contract_mobile)

if __name__ == '__main__':

    unittest.main()
